//
//  StatusOriginalView.swift
//  新浪微博
//
//  Created by MBP on 16/5/18.
//  Copyright © 2016年 MBP. All rights reserved.
//

import UIKit

/// 微博头像尺寸
private let PNStatusIconSize = CGSize(width: 36, height: 36)

class StatusOriginalView: UIView {

    //微博视图模型
    var viewModel: StatusViewModel? {
        didSet{
            //头像
            iconView.pn_setImageWithURL(
                viewModel?.status.user?.profile_image_url,
                placeholderName: "avatar_default_big",
                size: PNStatusIconSize,
                isCorner: true)
            
            //姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            
            //会员图标
            memberIconView.image = viewModel?.userMemberImage
            //认证图标
            vipIconView.image = viewModel?.userVipImage
            
            //来源文字
            sourceLabel.text = viewModel?.sourceText
            //时间
            timeLabel.text = viewModel?.createTime?.pn_dateDescription
            
            //正文
            contentLabel.text = viewModel?.status.text
            
            //更新配图约束
            picturesView.snp_updateConstraints { (make) in
                make.size.equalTo(viewModel!.picturesViewSize)
            }
            // 设置配图视图内容
            picturesView.pic_urls = viewModel?.status.pic_urls

        }
    }
        
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 头像图标
    private let iconView: UIImageView = UIImageView(pn_imageName: "avatar_default_big")
    /// 姓名标签
    private let nameLabel: UILabel = UILabel(pn_text: "赵日天")
    /// 会员图标
    private let memberIconView: UIImageView = UIImageView(pn_imageName: "common_icon_membership_level1")
    /// VIP 图标
    private let vipIconView: UIImageView = UIImageView(pn_imageName: "avatar_vip")
    /// 时间
    private let timeLabel: UILabel = UILabel(pn_text: "创建时间", fontSize: 10, textColor: PNAppearanceTintColor)
    /// 来源
    private let sourceLabel: UILabel = UILabel(pn_text: "来源 穷屌开发者", fontSize: 10)
    /// 内容标签
    private let contentLabel: UILabel = UILabel(pn_text: "微博微博", fontSize: 15)
    /// 配图视图
    private let picturesView: StatusPictureView = StatusPictureView()

}

extension StatusOriginalView {
    private func setupUI() {
        //背景色
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picturesView)
        
        
        // 2. 自动布局
        // 通用的间距
        let margin: CGFloat = 12.0
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(margin)
            make.left.equalTo(margin)
            make.size.equalTo(PNStatusIconSize)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.right.equalTo(self).offset(-margin)
            
        }
        picturesView.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            
            // TODO: - 默认不设置大小
            make.size.equalTo(CGSizeZero)
            
            // 很重要，设置底部约束，才能够设置cell的自动计算高度
            make.bottom.equalTo(self).offset(-margin)
        }

        
    }
}
